package com.tengju.bff.interfaces.manage.crm.staff.sentiment;

import com.tengju.bff.interfaces.covercharge.util.CsvExportUtil;
import com.tengju.bff.interfaces.covercharge.util.ExcelExportUtil;
import com.tengju.bff.interfaces.manage.crm.staff.sentiment.facade.StaffSentimentFacade;
import com.tengju.bff.interfaces.manage.crm.staff.sentiment.form.StaffSentimentCountListQuery;
import com.tengju.bff.interfaces.manage.crm.staff.sentiment.form.StaffSentimentCountPieQuery;
import com.tengju.bff.interfaces.manage.crm.staff.sentiment.form.StaffSentimentRecordListQuery;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.FormVO;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.render.ChartVO;
import com.tengju.bff.interfaces.shared.render.PieVO;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.data.domain.shared.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: 李大仙
 * @Date: 2021/4/21 2:30 下午
 */
@Validated
@RestController
@Api(tags = "运营人员客情信息")
@RequestMapping(value = "manage/crm/staff/sentiment")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class StaffSentimentController {

    private final StaffSentimentFacade staffSentimentFacade;

    @ApiOperation(value = "获取部门人员客情记录统计列表", notes = "获取部门人员客情记录统计列表")
    @RequestMapping(value = "/count/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<TableVO> countList(@Valid StaffSentimentCountListQuery query) {

        TableVO vo = staffSentimentFacade.getCountList(query);
        return ApiResponse.newSuccess(vo);
    }

    @ApiOperation(value = "导出部门人员客情记录统计列表", notes = "导出部门人员客情记录统计列表")
    @RequestMapping(value = "/count/export", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void exportCountList(@Valid StaffSentimentCountListQuery query){
        TableVO tableVO = staffSentimentFacade.exportRecordWithStatistics(query);
        HttpServletRequest request = ServletContextUtil.getRequest();
        HttpServletResponse response = ServletContextUtil.getResponse();
        ExcelExportUtil excel = new ExcelExportUtil();
        excel.create(0, tableVO);
        String fileName = "客情统计列表" + "_" + DateUtil.formatDayInt(LocalDateTime.now()) + ".xls";
        excel.downloadExcel(request, response, fileName);
    }


    @ApiOperation(value = "导出客情记录详情列表", notes = "导出客情记录详情列表")
    @RequestMapping(value = "/sentimentRecord/export", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void exportRecordList(@Valid StaffSentimentRecordListQuery query) {
        HttpServletRequest request = ServletContextUtil.getRequest();
        HttpServletResponse response = ServletContextUtil.getResponse();
        TableVO tableVO = staffSentimentFacade.exportRecordList(query);
        ExcelExportUtil excel = new ExcelExportUtil();
        excel.create(0, tableVO);
        String fileName = "客情记录详情列表" + "_" + DateUtil.formatDayInt(LocalDateTime.now()) + ".xls";
        excel.downloadExcel(request, response, fileName);
    }

    @ApiOperation(value = "查询部门人员客情记录信息列表", notes = "查询部门人员客情记录信息列表")
    @RequestMapping(value = "/sentimentRecord/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<TableVO> sentimentRecordPageList(@Valid StaffSentimentRecordListQuery query) {
        TableVO vo = staffSentimentFacade.getSentimentRecordPageList(query);
        return ApiResponse.newSuccess(vo);
    }

    @ApiOperation(value = "查看客情记录信息详情", notes = "查看客情记录信息详情")
    @RequestMapping(value = "/sentimentRecord/detail", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<FormVO> getSentimentRecordDetail(@RequestParam("sentimentId") Long sentimentId) {
        return ApiResponse.newSuccess(staffSentimentFacade.getSentimentRecordDetail(sentimentId));
    }

    @ApiOperation(value = "获取客情记录页添加信息模板", notes = "获取客情记录页添加信息模板")
    @RequestMapping(value = "/sentimentRecord/getRecordDefaultForm", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<FormVO> getSentimentRecordDefaultForm() {
        return ApiResponse.newSuccess(staffSentimentFacade.getSentimentRecordDefaultForm());
    }

    @ApiOperation(value = "获取客情记录页修改信息模板", notes = "获取客情记录页修改信息模板")
    @RequestMapping(value = "/sentimentRecord/getDefaultUpdateForm", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<FormVO> getDefaultUpdateForm(@RequestParam("sentimentId") Long sentimentId) {
        return ApiResponse.newSuccess(staffSentimentFacade.getDefaultUpdateForm(sentimentId));
    }

    @ApiOperation(value = "根据关键词(id,nickname,phoneNumber)获取服务商信息", notes = "根据关键词(id,nickname,phoneNumber)获取服务商信息")
    @RequestMapping(value = "/sentimentRecord/getServiceProviderInfoByKeywords", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<List<KVVO<String, Long>>> getServiceProviderInfoByKeywords(String serviceProviderIdAndNicknameAndMobile) {
        return ApiResponse.newSuccess(staffSentimentFacade.getServiceProviderInfoByKeywords(serviceProviderIdAndNicknameAndMobile));
    }

    @ApiOperation(value = "销售人员分布占比饼图", notes = "销售人员分布占比饼图")
    @RequestMapping(value = "/count/pie", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<PieVO> sentimentConsumerCountPie(@Valid StaffSentimentCountPieQuery query) {
        return ApiResponse.newSuccess(staffSentimentFacade.getSentimentCountPie(query));
    }

    @ApiOperation(value = "月客情记录趋势柱状图", notes = "月客情记录趋势柱状图")
    @RequestMapping(value = "/count/chart", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<ChartVO> sentimentRecordChartMonth(@NotNull(message = "月份不能为空") @RequestParam String month) {
        return ApiResponse.newSuccess(staffSentimentFacade.getSentimentRecordChartMonth(month));
    }

}
